package top.lingkang.finalgateway.admin.mapper;

import org.noear.solon.core.handle.Context;
import top.lingkang.finalgateway.admin.constants.LogType;
import top.lingkang.finalgateway.admin.entity.ActionLogEntity;
import top.lingkang.finalgateway.admin.param.LogParam;
import top.lingkang.mm.annotation.MagicMapper;
import top.lingkang.mm.orm.BaseMapper;

import java.util.List;

/**
 * @author lingkang
 * Created by 2024/4/28
 */
@MagicMapper
public interface ActionLogMapper extends BaseMapper<ActionLogEntity> {

    List<ActionLogEntity> list(LogParam param);

    /**
     * @param type {@link top.lingkang.finalgateway.admin.constants.LogType}
     * @param data data
     */
    default void addLog(LogType type, String data) {
        ActionLogEntity entity = new ActionLogEntity();
        entity.setType(type.getType());
        entity.setName(type.getName());
        entity.setData(data);
        Context current = Context.current();
        if (current != null)
            entity.setIp(current.realIp());
        insert(entity);
    }

    default void addLog(LogType type, String ip, String data) {
        ActionLogEntity entity = new ActionLogEntity();
        entity.setType(type.getType());
        entity.setName(type.getName());
        entity.setData(data);
        entity.setIp(ip);
        insert(entity);
    }
}
